package xmlParser;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class XMLParseUtil {
	private SaxContext sc ;
	private String cha = "utf-8";
	private String filepath;
	public XMLParseUtil(){
		
	}
	public XMLParseUtil(String xml, String cha) throws Exception{
		this.cha = cha != null && cha.length() > 0 ? cha : this.cha;
		XMLReader reader = XMLReaderFactory.createXMLReader();
		reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
		reader.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
		reader.setFeature("http://xml.org/sax/features/namespaces", false);
		reader.setFeature("http://xml.org/sax/features/validation", false);
		reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
		reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
		reader.setFeature("http://apache.org/xml/features/validation/schema", false);		
		SAXHandler0 handler = new SAXHandler0();
		reader.setContentHandler(handler);
		InputSource xmlSource = new InputSource(new ByteArrayInputStream(xml.getBytes(this.cha)));
		reader.parse(xmlSource);
		sc = new SaxContext(handler.fMap);
	}
	public void XMLParseUtilByFile(String filepath, String cha) throws Exception{
		this.filepath = filepath;
		this.cha = cha != null && cha.length() > 0 ? cha : this.cha;
		XMLReader reader = XMLReaderFactory.createXMLReader();
		reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
		reader.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
		reader.setFeature("http://xml.org/sax/features/namespaces", false);
		reader.setFeature("http://xml.org/sax/features/validation", false);
		reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
		reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
		reader.setFeature("http://apache.org/xml/features/validation/schema", false);		
		
		SAXHandler0 handler = new SAXHandler0();
		reader.setContentHandler(handler);
		File f = new File(this.filepath);
		FileInputStream is = new FileInputStream(f);
		InputSource xmlSource = new InputSource(new InputStreamReader( is , cha));
		reader.parse(xmlSource);
		sc = new SaxContext(handler.fMap);
	}
	public String query(String path){
		return sc.getNode(path).getText();
	}
	public String query(SAXBean saxb, String path){
		return sc.getNode(saxb, path).getText();
	}
	
	public List<SAXBean> queryNodes(String path){
		return sc.getNodes(path);
	}
	public List<SAXBean> queryNodes(SAXBean saxb, String path){
		return sc.getNodes(saxb, path);
	}
	
	/**
	 * <rss>
			<head id="jack">this is head!</head>
			<body>
				<node>no1</node>
				<node>no2</node>
				<node>no3</node>
			</body>
		</rss>
	 * @param arg
	 */
	public static void main(String[] arg){
		try{
			XMLParseUtil  xp = new XMLParseUtil();
			xp.XMLParseUtilByFile("e:\\kw.xml", "utf-8");
			List<SAXBean> list = xp.queryNodes("/rss/head");
			if(list != null && list.size() > 0){
				System.out.println("head =" + list.get(0).fValue);
				System.out.println("attr.id=" + list.get(0).getAttribute("id"));
				System.out.println("path=" + list.get(0).path);
			}

			List<SAXBean> list2 = xp.queryNodes("/rss/body/node");
			for(int x = 0 ; x < list2.size(); x++){
				System.out.println("content=" + list2.get(x).getText());
			}

			System.out.println("end");
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
}
